package com.why.blog.admin.comment;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.why.blog.common.R;
import com.why.blog.common.ResponseCode;
import com.why.blog.common.utils.AccessTerminalUtil;
import com.why.blog.common.utils.IPUtil;
import com.why.blog.entity.HaloComment;
import com.why.blog.entity.VO.CommentVO;
import com.why.blog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author TriggerWu
 * @version 1.0.0
 * @ClassName commentController.java
 * @Description 评论管理
 * @createTime 2019年05月02日 00:11:00
 */
@RestController
@RequestMapping("/comment")
public class commentController {

    @Autowired
    private CommentService commentService;


    @GetMapping("/")
    public ModelAndView tt() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("comment/list");
        return mv;
    }
    
      /**
       * @Title:
       * @Description: 发送评论
       * @Param:
       * @Return:
       * @Author: TriggerWu
       * @Date: 2019/5/6
       * @Time: 10:54
      **/
    @PostMapping("/send")
    public R saveSend(CommentVO co, @RequestParam("token") String token, HttpServletRequest request) {
        Map<String, String> map = AccessTerminalUtil.userAgentParse(request);
        String ipAddr = IPUtil.getIpAddr(request);
//        System.out.println(request.getRemoteAddr());
//        System.out.println(map.get("browserVersion"));
//        System.out.println(map.get("os"));
        if(!map.isEmpty()&&ipAddr!=null){
            co.setCommentAgent(map.toString());
            co.setCommentAuthorIp(ipAddr);
        }
        Integer res = commentService.saveComment(co, token);
        if (-1 == res) {
            return R.getInstance(ResponseCode.PARAMETER_ERROR.getCode(), ResponseCode.PARAMETER_ERROR.getMsg());
        }
        return R.getInstance(ResponseCode.RESPONSE_SUCCESS.getCode(), ResponseCode.RESPONSE_SUCCESS.getMsg());
    }

    @GetMapping("/del/{commentId}")
    public R removeId(@PathVariable("commentId") String commentId) {
        return commentService.removeComment(commentId);
    }

    /**
     * @title
     * @description 先做到这了 git吧我玩服了
     * @author TriggerWu
     * @updateTime 2019/5/2 3:25 
     * @throws 
     */
    @GetMapping("/showList")
    public Object showList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                      @RequestParam(value = "limit", defaultValue = "10") Integer pageSize,
                      CommentVO co) {
        IPage<HaloComment> iPage = commentService.showList(co, page, pageSize);
        JSONObject result = new JSONObject();
        if(iPage.getRecords().size()==0){
            result.put("code", "0");
            result.put("data", iPage.getRecords());
            result.put("msg", "没有更多数据");
            result.put("count", iPage.getTotal());
            return result;
        }
        result.put("code", "0");
        result.put("data", iPage.getRecords());
        result.put("msg", "");
        result.put("count", iPage.getTotal());
        return result;
    }

}
